package com.lgb.controller;

import com.lgb.Service.BlogService;
import com.lgb.Service.CommentService;
import com.lgb.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private BlogService blogService;

    //头像地址
     private String avatar = "/images/avatar.png";

    //返回片段，替换对应内容
    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId , Model model){
        model.addAttribute("comments" , commentService.listCommentByBlogId(blogId));
        return "blog :: commentList";
    }

    //接收信息并保存
    @PostMapping("/comments")
    public String post_comment(Comment comment){
        //获取传递而来的comment对应的blog的id
        Long blogId = comment.getBlog().getId();
        //先通过id查询对应的blog对象，再赋值给comment
        comment.setBlog(blogService.getBlog(blogId));
        //设置头像
        comment.setAvatar(avatar);
        commentService.saveComment(comment);
        return "redirect:/comments/" + blogId;
    }
}
